package com.huiquan.slang.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.slang.service.SlangRuleService;

@Controller
public class SlangRuleController {

	@Autowired
	private SlangRuleService slangRuleService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/slang/rule_index.htm")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		return slangRuleService.index(start);
	}

	@RequestMapping(value = "/slang/rule_pre_edit.htm")
	public ModelAndView rulePreEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String id = req.getParameter("id");
		String vocabulary = req.getParameter("vocabulary");
		User user = (User) req.getSession().getAttribute("user");
		return slangRuleService.rulePreEdit(start, id, vocabulary, user);
	}

	@RequestMapping(value = "/slang/rule_del.htm")
	public ModelAndView delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		Long id = Long.parseLong(req.getParameter("id"));
		return slangRuleService.delete(start, id);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/slang_rule_edit.do")
	public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String type = req.getParameter("rule_type");
		Long id = Long.parseLong(req.getParameter("id"));

		Map<String, Object> map = req.getParameterMap();

		String[] vocabularyStrs = (String[]) map.get("vocabulary");
		String[] characterStrs = (String[]) map.get("character");
		String[] propertyStrs = (String[]) map.get("property");
		String oldCharacter = req.getParameter("old_character");
		String oldProperty = req.getParameter("old_property");

		return slangRuleService.edit(id, vocabularyStrs, characterStrs, propertyStrs, user, type, oldCharacter,
				oldProperty);
	}
}
